package com.katze.boot.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.katze.boot.common.exception.BusinessException;
import com.katze.boot.plugins.security.annotations.RestControllerMapping;
import com.katze.boot.plugins.security.annotations.RestRequestMapping;
import com.katze.boot.system.domain.SysDict;
import com.katze.boot.system.service.DictService;
import com.katze.common.Message;
import com.katze.common.Pagination;
import com.katze.common.controller.BaseController;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestControllerMapping(value = "/dict")
public class DictController extends BaseController {

    @Resource
    private DictService dictService;

    @RestRequestMapping(permits = "dict:view", skip = true)
    public Message dictList(Pagination request, SysDict dict) {
        return toDataTable(this.dictService.findDicts(request, dict));
    }

    @RestRequestMapping(value = "/tree", permits = "dict:view", skip = true)
    public Message dictTreeList() {
        return toDataTable(this.dictService.findDicts());
    }

    @RestRequestMapping(value = "/trim", skip = true)
    @SuppressWarnings("unchecked")
    public Message dictTrimList(SysDict dict) {
        if (StringUtils.isBlank(dict.getTableName())) {
            return Message.error("表名信息不能为空");
        }
        List<SysDict> dicts = (List<SysDict>) this.dictService.findDicts(new Pagination(), dict);
        Map<String, LinkedList<Map<String,Object>>> data = new ConcurrentHashMap<>();
        for (int i = 0; i < dicts.size(); i++) {
            HashMap<String,Object> map=new HashMap<>();
            map.put("key",i);
            map.put("value", dicts.get(i).getValue());
            map.put("label", dicts.get(i).getDescription());
            data.computeIfAbsent(dicts.get(i).getFieldName(), (k) -> new LinkedList<>()).add(map);
        }
        return Message.ok().data(data);
    }

    @RestRequestMapping(method = RequestMethod.POST, permits = "dict:add", description = "新增字典")
    public Message addDict(SysDict dict) {
        this.dictService.createDict(dict);
        return Message.ok("新增字典成功");
    }

    @RestRequestMapping(value = "/tree", method = RequestMethod.POST, permits = "dict:add", description = "新增字典")
    public Message addDictTree(SysDict dict) {
        for (int i = 0; i < dict.getChildrenNum(); i++) {
            dict.setDictId(null);
            this.dictService.createDict(dict);
        }
        return Message.ok("新增字典成功");
    }

    @RestRequestMapping(method = RequestMethod.PUT, permits = "dict:update", description = "修改字典")
    public Message updateDict(SysDict dict) {
        this.dictService.updateDict(dict);
        return Message.ok("修改字典成功");
    }

    @RestRequestMapping(value = "/tree", method = RequestMethod.PUT, permits = "dict:update", description = "修改字典")
    public Message updateDictTree(SysDict dict) throws BusinessException {
        this.dictService.updateByCombinationId(dict);
        return Message.ok("修改字典成功");
    }

    @RestRequestMapping(method = RequestMethod.DELETE, permits = "dict:delete", description = "删除字典")
    public Message deleteDict(String... dictIds) {
        this.dictService.deleteDicts(dictIds);
        return Message.ok("删除字典成功");
    }

    @RestRequestMapping(value = "/tree", method = RequestMethod.DELETE, permits = "dict:delete", description = "删除字典")
    public Message deleteDictTree(String combinationId) {
        this.dictService.deleteByCombinationId(combinationId);
        return Message.ok("删除字典成功");
    }

    @RestRequestMapping(value = "/excel", method = RequestMethod.POST, permits = "dict:export", description = "导出字典")
    @SuppressWarnings("unchecked")
    public void export(Pagination request, SysDict dict, HttpServletResponse response) throws BusinessException {
        try {
            List<SysDict> dicts = (List<SysDict>) this.dictService.findDicts(request, dict);
            super.downXlsx("字典信息表", dicts, response);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败", e);
        }
    }
}